package com.vtquang.mygame2d.actor;

import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Broken extends MySprite2D{

	private float stateTime = 0;
	private Animation animation; 

	public Broken(TextureAtlas atlas, String name, int start, int end, float duration) { 
		super(atlas.findRegion(name, 1).getTexture());
		
		int count = end - start + 1;
		AtlasRegion[] regions = new AtlasRegion[count];
		for (int i = 0; i < count; i++) {
			regions[i] = new AtlasRegion(atlas.findRegion(name, start + i));
		} 
		
		animation = new Animation(duration, regions); 
	}

	@Override
	public void update(float gameTime) {
		stateTime += gameTime;
		TextureRegion region = animation.getKeyFrame(stateTime, false);
		if(region != null){
			texture = region.getTexture();
		}else{
			isAlive = false;
		}
	}
	
	@Override
	public void draw(SpriteBatch spriteBatch) { 
		if(isAlive){
			spriteBatch.draw(texture, position.x, position.y, size.x, size.y);
		}
	}
}
